package com.xlh.fgs;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xlh.Model.ColorConfModel;
import com.xlh.Utils.Constant;
import com.xlh.Utils.GsonManager;
import com.xlh.bean.AppColor;
import com.xlh.outside.MainActivity;
import com.xlh.outside.R;
import com.xlh.view.ColorPickerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建角色页面
 */

public class SetColorMainFragment extends Fragment {


	private Context context;

	public SetColorMainFragment() {
		// TODO Auto-generated constructor stub
	}

	public static final String TAG = SetColorMainFragment.class.getName();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.set_color_main, container, false);
	}

	//重写该方法 保证该页面加载时 总时刷新 最新的风格
	@Override
	public void onResume(){
		super.onResume();


		setActiveColor();
	}


	public void setActiveColor(){

		setControlColor(cEtBack,Constant.getAppColor().getBackColor());
		setControlColor(cEtBorderColor,Constant.getAppColor().getBorderColor());
		setControlColor(cEtButBackColor,Constant.getAppColor().getButBackColor());
		setControlColor(cEtButBorderColor,Constant.getAppColor().getButBorderColor());
		setControlColor(cEtLineColor,Constant.getAppColor().getLineColor());
		setControlColor(cEtFontColor,Constant.getAppColor().getFontColor());
		setControlColor(cEtAlertBoxBackColor,Constant.getAppColor().getAlertBoxBackColor());
		setControlColor(cEtAlertBoxBorderColor,Constant.getAppColor().getAlertBoxBorderColor());

	}



	@Override
	public void onViewCreated(final View view, Bundle savedInstanceState) {
			// TODO Auto-generated method stub
		//绑定控件
		cButSubmit  = (Button)view.findViewById(R.id.set_color_main_but_submit);
		cButReset  = (Button)view.findViewById(R.id.set_color_main_but_reset);

		cEtBack 		= (EditText)view.findViewById(R.id.back);
		cEtBorderColor 	= (EditText)view.findViewById(R.id.borderColor);
		cEtButBackColor =  (EditText)view.findViewById(R.id.butBackColor);
		cEtButBorderColor =  (EditText)view.findViewById(R.id.butBorderColor);
		cEtLineColor = (EditText)view.findViewById(R.id.lineColor);
		cEtFontColor = (EditText)view.findViewById(R.id.fontColor);
		cEtAlertBoxBackColor = (EditText)view.findViewById(R.id.alertBoxBackColor);
		cEtAlertBoxBorderColor = (EditText)view.findViewById(R.id.alertBoxBorderColor);



		ll = (LinearLayout) view.findViewById(R.id.ll_color);


		colorPickerView = new ColorPickerView(getActivity());
		ll.addView(colorPickerView);

		colorPickerView.setOnColorBackListener(new ColorPickerView.OnColorBackListener() {
				@Override
				public void onColorBack(int a, int r, int g, int b) {
					//tv.setText("R：" + r + "\nG：" + g + "\nB：" + b + "\n" + colorPickerView.getStrColor());
					//tv.setTextColor(Color.argb(a, r, g, b));
					//Constant.setAppColor();
					if(activeEditView!=null){
						activeEditView.setText( colorPickerView.getStrColor() );
						activeEditView.setBackgroundColor(Color.parseColor(colorPickerView.getStrColor() ));
					}


			}
		});



		//获取正在使用的颜色

		setElementList = new ArrayList<EditText>();

		setElementList.add(cEtBack);
		setElementList.add(cEtBorderColor);
		setElementList.add(cEtButBackColor);
		setElementList.add(cEtButBorderColor);
		setElementList.add(cEtLineColor);
		setElementList.add(cEtFontColor);
		setElementList.add(cEtAlertBoxBackColor);
		setElementList.add(cEtAlertBoxBorderColor);




		for(EditText e:setElementList){
			final EditText ele = e;
			//e.setEnabled(true);
			e.setFocusable(false);
			e.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					activeEditView = ele;

				}
			});
		}



		cButSubmit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				AppColor acl = new AppColor()
						.setBackColor(cEtBack.getText().toString())
						.setBorderColor(cEtBorderColor.getText().toString())
						.setButBackColor(cEtButBackColor.getText().toString())
						.setButBorderColor(cEtButBorderColor.getText().toString())
						.setLineColor(cEtLineColor.getText().toString())
						.setFontColor(cEtFontColor.getText().toString())
						.setAlertBoxBackColor(cEtAlertBoxBackColor.getText().toString())
						.setAlertBoxBorderColor(cEtAlertBoxBorderColor.getText().toString());

				Constant.setAppColor(acl);

				//调用更新颜色
				((MainActivity)getActivity()).setAppColor();
				((MainActivity)getActivity()).hideSetColorMain();
				//存入数据库
				String jsonData = GsonManager.getIns().objectToJsonStr(acl);

				new ColorConfModel(getActivity()).add("user1",jsonData);



			}
		});




		cButReset.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Constant.setAppColor((AppColor)Constant.getDefaultAppColor());

				setActiveColor();

				//调用更新颜色
				((MainActivity)getActivity()).setAppColor();
				((MainActivity)getActivity()).hideSetColorMain();


			}
		});




	}


	public void setControlColor(EditText e){

			String color = Constant.getAppColor().getBorderColor();

			e.setText( color );
			e.setTextColor(Color.parseColor(color ));


	}

	public void setControlColor(EditText e,String color){

		e.setText( color );
		//e.setTextColor(Color.parseColor(color ));
		e.setBackgroundColor(Color.parseColor(color ));

	}


	//定义一堆控件先
	/*保存配色*/
	private Button cButSubmit;

	/*恢复默认配色*/
	private Button cButReset;
	/*主体背景色*/
	private EditText cEtBack;
	/*主体边框色*/
	private EditText cEtBorderColor;

	/*按钮背景色*/
	private EditText cEtButBackColor;

	/*按钮边框色*/
	private EditText cEtButBorderColor;
	/*主体线条色*/
	private EditText cEtLineColor;
	/*字体色*/
	private EditText cEtFontColor;

	/*弹出框背景色*/
	private EditText cEtAlertBoxBackColor;

	/*弹出框边框色*/
	private EditText cEtAlertBoxBorderColor;


	private List<EditText> setElementList;

	public EditText incle;

	public LinearLayout ll;
	public TextView tv;
	public ColorPickerView colorPickerView;
	public EditText activeEditView;


}
